/*
 * Copyright (c) 2024. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

package com.zhipin.planck;

/**
 * @author xieqing
 * @email xieqing@kanzhun.com
 * @description
 * @date 2024-02-20 17:03
 */
public class ChannelSessionUtil {

    public static String getIpByChannelId(long cid) {
        long ip = cid >> 28;
        return getIpStringFromInt((int) ip);
    }

    public static String getIpStringFromInt(int ip) {
        return getIpStringFromBytes(int2Byte(ip));
    }

    /**
     * @param ip ip的字节数组形式
     * @return 字符串形式的ip
     */
    public static String getIpStringFromBytes(byte[] ip) {
        StringBuffer sb = new StringBuffer();
        sb.append(ip[0] & 0xFF);
        sb.append('.');
        sb.append(ip[1] & 0xFF);
        sb.append('.');
        sb.append(ip[2] & 0xFF);
        sb.append('.');
        sb.append(ip[3] & 0xFF);
        return sb.toString();
    }

    public static byte[] int2Byte(int intValue) {
        byte[] b = new byte[4];
        for (int i = 0; i < 4; i++) {
            b[i] = (byte) (intValue >> 8 * (3 - i) & 0xff);
        }
        return b;
    }

    public static void main(String[] args) {
        long cid = 1198861004969579417l;
        System.out.println(cid);
        System.out.println(ChannelSessionUtil.getIpByChannelId(cid));
    }

}